<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="深山磨柴斧，一鸣震江湖">
  <link rel="icon" href="/img/ic_head.jpg">
  <title>027-Android存储（2）：文件&Properties详解</title>
  
  
  <meta property="og:title" content="027-Android存储（2）：文件&amp;Properties详解">
  
  
  <meta property="og:url" content="http://example.com/2019/09/28/027-Android%E5%AD%98%E5%82%A8%EF%BC%882%EF%BC%89%EF%BC%9A%E6%96%87%E4%BB%B6&amp;Properties%E8%AF%A6%E8%A7%A3/index.html">
  
  
  <meta property="og:img" content="/img/ic_head.jpg">
  
  
  <meta property="og:img" content="深山磨柴斧，一鸣震江湖">
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2019-09-28">
  <meta property="og:article:modified_time" content="2021-04-14">
  <meta property="og:article:author" content="Cupster">
  
  
  <meta property="og:article:tag" content="Android">
  
  <meta property="og:article:tag" content="Android持久化">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/ic_head.jpg" alt="logo">
      
      <span class="navbar-logo-dsc">不负明天</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      027-Android存储（2）：文件&Properties详解
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2019-09-28T04:13:22.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2019-09-28</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/Android/" class="post-meta-link">Android</a>
    
    
    
    <span class="dot"></span>
    <span>3k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/Android/" class="post-meta-link">Android</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/Android持久化/" class="post-meta-link">Android持久化</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <p><a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/article/details/113869112">【5年Android从零复盘系列之二十七】Android存储（2）：文件&amp;Properties</a></p>
<hr />

<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Cupster/article/details/112653858">【代码是最好的老师】</a></p>
<h1 id="1-概述"><a href="#1-概述" class="headerlink" title="1.概述"></a>1.概述</h1><p>Android中文件操作与Java一致，File类是java中的文件操作工具类，Android其他文件操作API也是基于file操作进行的封装。</p>
<p>文件操作有：</p>
<ol>
<li>File</li>
<li>Properties</li>
<li>assets原始文件</li>
<li>raw原始文件</li>
</ol>
<h1 id="2-File存储与读取"><a href="#2-File存储与读取" class="headerlink" title="2.File存储与读取"></a>2.File存储与读取</h1><p>文件有关的操作，一般需要配置app权限，代码中需设置动态申请权限。</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.READ_EXTERNAL_STORAG&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.MOUNT_UNMOUNT_FILESYSTEMS&quot;</span> /&gt;</span></span><br></pre>



<pre class="highlight"><span class="line"><span class="function"><span class="keyword">private</span>  <span class="keyword">void</span>     <span class="title">checkPermission</span><span class="params">()</span></span>&#123;</span><br><span class="line">    PermissionUtil.applyPermission(<span class="keyword">this</span> ,<span class="number">101</span>, getApplication().getPackageName());</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>

<h1 id="2-1字符串写入File"><a href="#2-1字符串写入File" class="headerlink" title="2.1字符串写入File"></a>2.1字符串写入File</h1><p>字符串写入文件，一般使用FileOutputStream即可</p>
<p>封装方法如下：</p>
<pre class="highlight"><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">storeTxt</span><span class="params">(String path, String txtStr)</span> </span>&#123;</span><br><span class="line">        FileOutputStream fos = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            fos = <span class="keyword">new</span> FileOutputStream(path);</span><br><span class="line">            fos.write(txtStr.getBytes());</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (fos != <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    fos.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre>

<h1 id="2-2读取File文件中的字符串"><a href="#2-2读取File文件中的字符串" class="headerlink" title="2.2读取File文件中的字符串"></a>2.2读取File文件中的字符串</h1><p>从文件读取字符串，一般用FileInputStream</p>
<p>封装方法如下：</p>
<pre class="highlight"><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">readTxt</span><span class="params">(String path)</span></span>&#123;</span><br><span class="line">       String readStr =<span class="string">&quot;&quot;</span>;</span><br><span class="line">       FileInputStream fis =<span class="keyword">null</span>;</span><br><span class="line">       <span class="keyword">try</span> &#123;</span><br><span class="line">           fis = <span class="keyword">new</span> FileInputStream(path);</span><br><span class="line">           <span class="keyword">byte</span>[] bytes = <span class="keyword">new</span> <span class="keyword">byte</span>[fis.available()];</span><br><span class="line">           fis.read(bytes);</span><br><span class="line">           readStr = <span class="keyword">new</span> String(bytes);</span><br><span class="line">       &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">           e.printStackTrace();</span><br><span class="line">       &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">           <span class="keyword">if</span> (fis != <span class="keyword">null</span>)&#123;</span><br><span class="line">               <span class="keyword">try</span> &#123;</span><br><span class="line">                   fis.close();</span><br><span class="line">               &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                   e.printStackTrace();</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> readStr;</span><br><span class="line">   &#125;</span><br></pre>

<h1 id="2-3存储图片"><a href="#2-3存储图片" class="headerlink" title="2.3存储图片"></a>2.3存储图片</h1><p>图片的字节数据相对较大，选择io性能更好的BufferedOutputStream和BufferedInputStream</p>
<pre class="highlight"><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">storeBitmap</span><span class="params">(String path , Bitmap bitmap ,<span class="keyword">int</span> quality)</span></span>&#123;</span><br><span class="line">        BufferedOutputStream bos= <span class="keyword">null</span>;</span><br><span class="line">        FileOutputStream fos=<span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            fos = <span class="keyword">new</span> FileOutputStream(path);</span><br><span class="line">            bos = <span class="keyword">new</span> BufferedOutputStream(fos);</span><br><span class="line">            bitmap.compress(Bitmap.CompressFormat.JPEG,quality ,bos);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (fos !=<span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    fos.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (bos != <span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    bos.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre>

<h1 id="2-4读取图片"><a href="#2-4读取图片" class="headerlink" title="2.4读取图片"></a>2.4读取图片</h1><p>图片的字节数据相对较大，选择io性能更好的BufferedOutputStream和BufferedInputStream</p>
<pre class="highlight"><span class="line"><span class="function"><span class="keyword">public</span>  <span class="keyword">static</span>  Bitmap <span class="title">loadBitmap</span><span class="params">(String path )</span></span>&#123;</span><br><span class="line">       Bitmap bitmap = <span class="keyword">null</span> ;</span><br><span class="line">       FileInputStream fis = <span class="keyword">null</span>;</span><br><span class="line">       BufferedInputStream bis = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line">       <span class="keyword">try</span> &#123;</span><br><span class="line">           fis = <span class="keyword">new</span> FileInputStream(path);</span><br><span class="line">           bis = <span class="keyword">new</span> BufferedInputStream(fis);</span><br><span class="line">           bitmap = BitmapFactory.decodeStream(bis);</span><br><span class="line">       &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">           e.printStackTrace();</span><br><span class="line">       &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">           <span class="keyword">if</span> (fis != <span class="keyword">null</span>)&#123;</span><br><span class="line">               <span class="keyword">try</span> &#123;</span><br><span class="line">                   fis.close();</span><br><span class="line">               &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                   e.printStackTrace();</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="keyword">if</span> (bis != <span class="keyword">null</span>)&#123;</span><br><span class="line">               <span class="keyword">try</span> &#123;</span><br><span class="line">                   bis.close();</span><br><span class="line">               &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                   e.printStackTrace();</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> bitmap;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br></pre>

<h1 id="3-Properties存储与读取"><a href="#3-Properties存储与读取" class="headerlink" title="3.Properties存储与读取"></a>3.Properties存储与读取</h1><table>
<thead>
<tr>
<th>方法</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>load（）</td>
<td>加载Properties对象</td>
</tr>
<tr>
<td>store（）</td>
<td>保存到Properties文件</td>
</tr>
<tr>
<td>getProperty（）</td>
<td>获取属性值</td>
</tr>
<tr>
<td>setProperty</td>
<td>设置属性值</td>
</tr>
<tr>
<td>loadFromXML</td>
<td>从XML格式的属性文件中加载Properties对象</td>
</tr>
<tr>
<td>storeToXML</td>
<td>把属性对象保存到XML格式的Properties文件</td>
</tr>
</tbody></table>
<p>存储Properties</p>
<pre class="highlight"><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">storeProp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">       <span class="keyword">if</span> (FileStoreHelper.isExStoreUsable())&#123;</span><br><span class="line">           PropertiesHelper.getInstance(<span class="keyword">this</span>).init().open().writeString(<span class="string">&quot;auto_update&quot;</span>,<span class="string">&quot;yes&quot;</span>);</span><br><span class="line">       &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">           toast(<span class="string">&quot;无存储空间或权限&quot;</span>);</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre>


<p>读取Properties</p>
<pre class="highlight"><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">readProp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">       <span class="keyword">if</span> (FileStoreHelper.isExStoreUsable())&#123;</span><br><span class="line">           String s = PropertiesHelper.getInstance(<span class="keyword">this</span>).init().readString(<span class="string">&quot;auto_update&quot;</span>, <span class="string">&quot;no&quot;</span>);</span><br><span class="line">           tv.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">           tv.append(<span class="string">&quot;auto_update:&quot;</span>);</span><br><span class="line">           tv.append(s);</span><br><span class="line">       &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">           toast(<span class="string">&quot;无存储空间或权限&quot;</span>);</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre>

<p>PropertiesHelper.java</p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.androidfile;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.content.Context;</span><br><span class="line"><span class="keyword">import</span> android.os.Environment;</span><br><span class="line"><span class="keyword">import</span> android.util.Log;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.OutputStream;</span><br><span class="line"><span class="keyword">import</span> java.util.Properties;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PropertiesHelper</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">&quot;PropertiesHelper&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Context mContext;</span><br><span class="line">    <span class="keyword">private</span> String mPath;</span><br><span class="line">    <span class="keyword">private</span> String mFile;</span><br><span class="line">    <span class="keyword">private</span> Properties mProp;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> PropertiesHelper mPropUtil = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> PropertiesHelper <span class="title">getInstance</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (mPropUtil == <span class="keyword">null</span>) &#123;</span><br><span class="line">            mPropUtil = <span class="keyword">new</span> PropertiesHelper();</span><br><span class="line">            mPropUtil.mContext = context;</span><br><span class="line">            mPropUtil.mPath = Environment.getExternalStorageDirectory() + <span class="string">&quot;/ExmKeyValue&quot;</span>;</span><br><span class="line">            mPropUtil.mFile = <span class="string">&quot;properties.ini&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> mPropUtil;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> PropertiesHelper <span class="title">setPath</span><span class="params">(String path)</span> </span>&#123;</span><br><span class="line">        mPath = path;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> PropertiesHelper <span class="title">setFile</span><span class="params">(String file)</span> </span>&#123;</span><br><span class="line">        mFile = file;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> PropertiesHelper <span class="title">init</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Log.d(TAG, <span class="string">&quot;path=&quot;</span>+mPath+<span class="string">&quot;/&quot;</span>+mFile);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            File dir = <span class="keyword">new</span> File(mPath);</span><br><span class="line">            <span class="keyword">if</span> (!dir.exists()) &#123;</span><br><span class="line">                dir.mkdirs();</span><br><span class="line">            &#125;</span><br><span class="line">            File file = <span class="keyword">new</span> File(dir, mFile);</span><br><span class="line">            <span class="keyword">if</span> (!file.exists()) &#123;</span><br><span class="line">                file.createNewFile();</span><br><span class="line">            &#125;</span><br><span class="line">            InputStream is = <span class="keyword">new</span> FileInputStream(file);</span><br><span class="line">            mProp = <span class="keyword">new</span> Properties();</span><br><span class="line">            mProp.load(is);</span><br><span class="line">            is.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">commit</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            File file = <span class="keyword">new</span> File(mPath + <span class="string">&quot;/&quot;</span> + mFile);</span><br><span class="line">            OutputStream os = <span class="keyword">new</span> FileOutputStream(file);</span><br><span class="line">            mProp.store(os, <span class="string">&quot;&quot;</span>);</span><br><span class="line">            os.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        mProp.clear();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">clear</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        mProp.clear();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> PropertiesHelper <span class="title">open</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        mProp.clear();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            File file = <span class="keyword">new</span> File(mPath + <span class="string">&quot;/&quot;</span> + mFile);</span><br><span class="line">            InputStream is = <span class="keyword">new</span> FileInputStream(file);</span><br><span class="line">            mProp = <span class="keyword">new</span> Properties();</span><br><span class="line">            mProp.load(is);</span><br><span class="line">            is.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">writeString</span><span class="params">(String name, String value)</span> </span>&#123;</span><br><span class="line">        mProp.setProperty(name, value);</span><br><span class="line">        commit();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">readString</span><span class="params">(String name, String defaultValue)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> mProp.getProperty(name, defaultValue);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">writeInt</span><span class="params">(String name, <span class="keyword">int</span> value)</span> </span>&#123;</span><br><span class="line">        mProp.setProperty(name, <span class="string">&quot;&quot;</span>+value);</span><br><span class="line">        commit();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">readInt</span><span class="params">(String name, <span class="keyword">int</span> defaultValue)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Integer.parseInt(mProp.getProperty(name, <span class="string">&quot;&quot;</span>+defaultValue));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">writeBoolean</span><span class="params">(String name, <span class="keyword">boolean</span> value)</span> </span>&#123;</span><br><span class="line">        mProp.setProperty(name, <span class="string">&quot;&quot;</span>+value);</span><br><span class="line">        commit();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">readBoolean</span><span class="params">(String name, <span class="keyword">boolean</span> defaultValue)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Boolean.parseBoolean(mProp.getProperty(name, <span class="string">&quot;&quot;</span>+defaultValue));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">writeDouble</span><span class="params">(String name, <span class="keyword">double</span> value)</span> </span>&#123;</span><br><span class="line">        mProp.setProperty(name, <span class="string">&quot;&quot;</span>+value);</span><br><span class="line">        commit();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">readDouble</span><span class="params">(String name, <span class="keyword">double</span> defaultValue)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Double.parseDouble(mProp.getProperty(name, <span class="string">&quot;&quot;</span>+defaultValue));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>

<h1 id="4-其他相关代码"><a href="#4-其他相关代码" class="headerlink" title="4.其他相关代码"></a>4.其他相关代码</h1><p>MainActivity.java</p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.androidfile;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> androidx.annotation.NonNull;</span><br><span class="line"><span class="keyword">import</span> androidx.appcompat.app.AppCompatActivity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.content.pm.PackageManager;</span><br><span class="line"><span class="keyword">import</span> android.os.Bundle;</span><br><span class="line"><span class="keyword">import</span> android.os.Environment;</span><br><span class="line"><span class="keyword">import</span> android.view.View;</span><br><span class="line"><span class="keyword">import</span> android.widget.TextView;</span><br><span class="line"><span class="keyword">import</span> android.widget.Toast;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    TextView tv;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_main);</span><br><span class="line"></span><br><span class="line">        findViewById(R.id.btn_store_file).setOnClickListener(<span class="keyword">new</span> View.OnClickListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View view)</span> </span>&#123;</span><br><span class="line">                storeFile(<span class="string">&quot;这是存储的一段话&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        findViewById(R.id.btn_read_file).setOnClickListener(<span class="keyword">new</span> View.OnClickListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View view)</span> </span>&#123;</span><br><span class="line">                readFile();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        findViewById(R.id.btn_store_prop).setOnClickListener(<span class="keyword">new</span> View.OnClickListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View view)</span> </span>&#123;</span><br><span class="line">                storeProp();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        findViewById(R.id.btn_read_prop).setOnClickListener(<span class="keyword">new</span> View.OnClickListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View view)</span> </span>&#123;</span><br><span class="line">                readProp();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">       tv= findViewById(R.id.tv_content);</span><br><span class="line"></span><br><span class="line">        checkPermission();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span>  <span class="keyword">void</span>     <span class="title">checkPermission</span><span class="params">()</span></span>&#123;</span><br><span class="line">        PermissionUtil.applyPermission(<span class="keyword">this</span> ,<span class="number">101</span>, getApplication().getPackageName());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onRequestPermissionsResult</span><span class="params">(<span class="keyword">int</span> requestCode, <span class="meta">@NonNull</span> String[] permissions, <span class="meta">@NonNull</span> <span class="keyword">int</span>[] grantResults)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">switch</span> (requestCode )&#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">101</span>:</span><br><span class="line">                <span class="keyword">if</span> (grantResults.length &gt;<span class="number">0</span> &amp;&amp; grantResults[<span class="number">0</span>] == PackageManager.PERMISSION_GRANTED)&#123;</span><br><span class="line"></span><br><span class="line">                &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                    toast(<span class="string">&quot;存储读取权限限制&quot;</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">storeFile</span><span class="params">(String str)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (FileStoreHelper.isExStoreUsable())&#123;</span><br><span class="line">            FileStoreHelper.storeTxt(Environment.getExternalStorageDirectory().toString() + <span class="string">&quot;/file.txt&quot;</span> ,str);</span><br><span class="line">            toast(<span class="string">&quot;已保存&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            toast(<span class="string">&quot;无存储空间或权限&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">readFile</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (FileStoreHelper.isExStoreUsable())&#123;</span><br><span class="line">            String s = FileStoreHelper.readTxt(Environment.getExternalStorageDirectory().toString() + <span class="string">&quot;/file.txt&quot;</span> );</span><br><span class="line">            tv.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line"></span><br><span class="line">            tv.append(<span class="string">&quot;read_txt:&quot;</span>);</span><br><span class="line">            tv.append(s);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            toast(<span class="string">&quot;无存储空间或权限&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">storeProp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (FileStoreHelper.isExStoreUsable())&#123;</span><br><span class="line">            PropertiesHelper.getInstance(<span class="keyword">this</span>).init().open().writeString(<span class="string">&quot;auto_update&quot;</span>,<span class="string">&quot;yes&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            toast(<span class="string">&quot;无存储空间或权限&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">readProp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (FileStoreHelper.isExStoreUsable())&#123;</span><br><span class="line">            String s = PropertiesHelper.getInstance(<span class="keyword">this</span>).init().readString(<span class="string">&quot;auto_update&quot;</span>, <span class="string">&quot;no&quot;</span>);</span><br><span class="line">            tv.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">            tv.append(<span class="string">&quot;auto_update:&quot;</span>);</span><br><span class="line">            tv.append(s);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            toast(<span class="string">&quot;无存储空间或权限&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">toast</span><span class="params">(String text)</span></span>&#123;</span><br><span class="line">        Toast.makeText(<span class="keyword">this</span> ,text ,Toast.LENGTH_SHORT).show();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>



<p>PermissionUtil.java</p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.androidfile;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.Manifest.permission;</span><br><span class="line"><span class="keyword">import</span> android.app.Activity;</span><br><span class="line"><span class="keyword">import</span> android.content.ComponentName;</span><br><span class="line"><span class="keyword">import</span> android.content.Context;</span><br><span class="line"><span class="keyword">import</span> android.content.Intent;</span><br><span class="line"><span class="keyword">import</span> android.content.pm.PackageManager;</span><br><span class="line"><span class="keyword">import</span> android.net.Uri;</span><br><span class="line"><span class="keyword">import</span> android.os.Build;</span><br><span class="line"><span class="keyword">import</span> android.provider.Settings;</span><br><span class="line"><span class="keyword">import</span> android.text.TextUtils;</span><br><span class="line"><span class="keyword">import</span> android.util.Log;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> androidx.core.app.ActivityCompat;</span><br><span class="line"><span class="keyword">import</span> androidx.core.content.ContextCompat;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *  Encoding by Cupster on 2019/12/24 16:27.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 动态申请权限工具类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">PermissionUtil</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] GROUP_CALENDAR      = &#123;</span><br><span class="line">            permission.READ_CALENDAR, permission.WRITE_CALENDAR</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] GROUP_CAMERA        = &#123;</span><br><span class="line">            permission.CAMERA</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] GROUP_CONTACTS      = &#123;</span><br><span class="line">            permission.READ_CONTACTS, permission.WRITE_CONTACTS, permission.GET_ACCOUNTS</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] GROUP_LOCATION      = &#123;</span><br><span class="line">            permission.ACCESS_FINE_LOCATION, permission.ACCESS_COARSE_LOCATION</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] GROUP_MICROPHONE    = &#123;</span><br><span class="line">            permission.RECORD_AUDIO</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] GROUP_PHONE         = &#123;</span><br><span class="line">            permission.READ_PHONE_STATE, permission.READ_PHONE_NUMBERS, permission.CALL_PHONE,</span><br><span class="line">            permission.READ_CALL_LOG, permission.WRITE_CALL_LOG, permission.ADD_VOICEMAIL,</span><br><span class="line">            permission.USE_SIP, permission.PROCESS_OUTGOING_CALLS, permission.ANSWER_PHONE_CALLS</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] GROUP_PHONE_BELOW_O = &#123;</span><br><span class="line">            permission.READ_PHONE_STATE, permission.READ_PHONE_NUMBERS, permission.CALL_PHONE,</span><br><span class="line">            permission.READ_CALL_LOG, permission.WRITE_CALL_LOG, permission.ADD_VOICEMAIL,</span><br><span class="line">            permission.USE_SIP, permission.PROCESS_OUTGOING_CALLS</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] GROUP_SENSORS       = &#123;</span><br><span class="line">            permission.BODY_SENSORS</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] GROUP_SMS           = &#123;</span><br><span class="line">            permission.SEND_SMS, permission.RECEIVE_SMS, permission.READ_SMS,</span><br><span class="line">            permission.RECEIVE_WAP_PUSH, permission.RECEIVE_MMS,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] GROUP_STORAGE       = &#123;</span><br><span class="line">            permission.READ_EXTERNAL_STORAGE, permission.WRITE_EXTERNAL_STORAGE,</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 申请所有权限</span></span><br><span class="line"><span class="comment">     * 调用后，activity中复写 onRequestPermissionsResult（）处理申请权限结果</span></span><br><span class="line"><span class="comment">     * PERMISSION_DENIED = -1;</span></span><br><span class="line"><span class="comment">     * PERMISSION_GRANTED = 0;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> context activity</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> requestCode 请求码</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">applyPermission</span><span class="params">(Activity context ,<span class="keyword">int</span> requestCode ,String pkg)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (needApply(context ,pkg)) &#123;</span><br><span class="line">            String[] allpermissions =&#123;&#125;;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                allpermissions = context.getPackageManager().getPackageInfo(pkg, PackageManager.GET_PERMISSIONS)</span><br><span class="line">                        .requestedPermissions;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (PackageManager.NameNotFoundException e) &#123;</span><br><span class="line">                Log.e(<span class="string">&quot;&quot;</span>, <span class="string">&quot;app has no request any permission&quot;</span> );</span><br><span class="line">            &#125;</span><br><span class="line">            ActivityCompat.requestPermissions( context, allpermissions, requestCode);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 申请指定权限</span></span><br><span class="line"><span class="comment">     * 调用后，activity中复写 onRequestPermissionsResult（）处理申请权限结果</span></span><br><span class="line"><span class="comment">     * PERMISSION_DENIED = -1;</span></span><br><span class="line"><span class="comment">     * PERMISSION_GRANTED = 0;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> context activity</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> permissions 指定的权限</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">applySignPermission</span><span class="params">(Activity context,<span class="keyword">int</span> requestCode ,String[] permissions )</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!checkPermissions(context ,permissions)) &#123;</span><br><span class="line">            ActivityCompat.requestPermissions( context, permissions, requestCode);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isGranted</span><span class="params">(<span class="keyword">final</span> String... permissions)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (String permission : permissions) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!isGranted(permission)) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isGranted</span><span class="params">(Context context ,<span class="keyword">final</span> String permission)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.M</span><br><span class="line">                || PackageManager.PERMISSION_GRANTED</span><br><span class="line">                == ContextCompat.checkSelfPermission(context.getApplicationContext(), permission);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断是否有权限限制</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> context activity</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> pkg 包名</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 是否需要申请放行权限</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">needApply</span><span class="params">(Activity context ,String pkg )</span></span>&#123;</span><br><span class="line">        <span class="keyword">boolean</span> needApply = <span class="keyword">false</span>;</span><br><span class="line">        <span class="keyword">if</span> (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) &#123;</span><br><span class="line">            String[] allPermissions =&#123;&#125;;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                allPermissions = context.getPackageManager().getPackageInfo(pkg, PackageManager.GET_PERMISSIONS)</span><br><span class="line">                        .requestedPermissions;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (PackageManager.NameNotFoundException e) &#123;</span><br><span class="line">                Log.e(<span class="string">&quot;&quot;</span>, <span class="string">&quot;app has no request any permission&quot;</span> );</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (String permission : allPermissions) &#123;</span><br><span class="line">                <span class="keyword">int</span> checkPermission = ContextCompat.checkSelfPermission(context.getApplicationContext(),</span><br><span class="line">                        permission);</span><br><span class="line">                <span class="keyword">if</span> (checkPermission != PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">                    needApply = <span class="keyword">true</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> needApply;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 检测权限是否授权</span></span><br><span class="line"><span class="comment">     * Activity.onRequestPermissionsResult（）中回调使用</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> permissions 权限数组</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> true 有权限   false 被拒绝</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">checkPermissions</span><span class="params">(Context activity ,String[] permissions)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.M) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (String permission : permissions) &#123;</span><br><span class="line">            <span class="keyword">if</span> (ActivityCompat.checkSelfPermission(activity, permission) !=</span><br><span class="line">                    PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">skipPermissionSetting</span><span class="params">(Context context)</span></span>&#123;</span><br><span class="line"><span class="comment">//        String sdk = android.os.Build.VERSION.SDK; // SDK号</span></span><br><span class="line"><span class="comment">//        String model = android.os.Build.MODEL; // 手机型号</span></span><br><span class="line"><span class="comment">//        String release = android.os.Build.VERSION.RELEASE; // android系统版本号</span></span><br><span class="line">        String brand = Build.BRAND;<span class="comment">//手机厂商</span></span><br><span class="line">        <span class="keyword">if</span> (TextUtils.equals(brand.toLowerCase(), <span class="string">&quot;redmi&quot;</span>) || TextUtils.equals(brand.toLowerCase(), <span class="string">&quot;xiaomi&quot;</span>)) &#123;</span><br><span class="line">            gotoMiuiPermission(context);<span class="comment">//小米</span></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (TextUtils.equals(brand.toLowerCase(), <span class="string">&quot;meizu&quot;</span>)) &#123;</span><br><span class="line">            gotoMeizuPermission(context);</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (TextUtils.equals(brand.toLowerCase(), <span class="string">&quot;huawei&quot;</span>) || TextUtils.equals(brand.toLowerCase(), <span class="string">&quot;honor&quot;</span>)) &#123;</span><br><span class="line">            gotoHuaweiPermission(context);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            context.startActivity(getAppDetailSettingIntent(context));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 跳转到miui的权限管理页面</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">gotoMiuiPermission</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123; <span class="comment">// MIUI 8</span></span><br><span class="line">            Intent localIntent = <span class="keyword">new</span> Intent(<span class="string">&quot;miui.intent.action.APP_PERM_EDITOR&quot;</span>);</span><br><span class="line">            localIntent.setClassName(<span class="string">&quot;com.miui.securitycenter&quot;</span>, <span class="string">&quot;com.miui.permcenter.permissions.PermissionsEditorActivity&quot;</span>);</span><br><span class="line">            localIntent.putExtra(<span class="string">&quot;extra_pkgname&quot;</span>, context.getPackageName());</span><br><span class="line">            context.startActivity(localIntent);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123; <span class="comment">// MIUI 5/6/7</span></span><br><span class="line">                Intent localIntent = <span class="keyword">new</span> Intent(<span class="string">&quot;miui.intent.action.APP_PERM_EDITOR&quot;</span>);</span><br><span class="line">                localIntent.setClassName(<span class="string">&quot;com.miui.securitycenter&quot;</span>, <span class="string">&quot;com.miui.permcenter.permissions.AppPermissionsEditorActivity&quot;</span>);</span><br><span class="line">                localIntent.putExtra(<span class="string">&quot;extra_pkgname&quot;</span>, context.getPackageName());</span><br><span class="line">                context.startActivity(localIntent);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e1) &#123; <span class="comment">// 否则跳转到应用详情</span></span><br><span class="line">                context.startActivity(getAppDetailSettingIntent(context));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 跳转到魅族的权限管理系统</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">gotoMeizuPermission</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Intent intent = <span class="keyword">new</span> Intent(<span class="string">&quot;com.meizu.safe.security.SHOW_APPSEC&quot;</span>);</span><br><span class="line">            intent.addCategory(Intent.CATEGORY_DEFAULT);</span><br><span class="line">            intent.putExtra(<span class="string">&quot;packageName&quot;</span>, context.getPackageName());</span><br><span class="line">            context.startActivity(intent);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            context.startActivity(getAppDetailSettingIntent(context));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 华为的权限管理页面</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">gotoHuaweiPermission</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Intent intent = <span class="keyword">new</span> Intent();</span><br><span class="line">            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);</span><br><span class="line">            ComponentName comp = <span class="keyword">new</span> ComponentName(<span class="string">&quot;com.huawei.systemmanager&quot;</span>, <span class="string">&quot;com.huawei.permissionmanager.ui.MainActivity&quot;</span>);<span class="comment">//华为权限管理</span></span><br><span class="line">            intent.setComponent(comp);</span><br><span class="line">            context.startActivity(intent);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            context.startActivity(getAppDetailSettingIntent(context));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取应用详情页面intent（如果找不到要跳转的界面，也可以先把用户引导到系统设置页面）</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> Intent</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> Intent <span class="title">getAppDetailSettingIntent</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">        Intent localIntent = <span class="keyword">new</span> Intent();</span><br><span class="line">        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);</span><br><span class="line">        <span class="keyword">if</span> (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.GINGERBREAD) &#123;</span><br><span class="line">            localIntent.setAction(<span class="string">&quot;android.settings.APPLICATION_DETAILS_SETTINGS&quot;</span>);</span><br><span class="line">            localIntent.setData(Uri.fromParts(<span class="string">&quot;package&quot;</span>,context.getPackageName(), <span class="keyword">null</span>));</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (Build.VERSION.SDK_INT &lt;= Build.VERSION_CODES.FROYO) &#123;</span><br><span class="line">            localIntent.setAction(Intent.ACTION_VIEW);</span><br><span class="line">            localIntent.setClassName(<span class="string">&quot;com.android.settings&quot;</span>, <span class="string">&quot;com.android.settings.InstalledAppDetails&quot;</span>);</span><br><span class="line">            localIntent.putExtra(<span class="string">&quot;com.android.settings.ApplicationPkgName&quot;</span>,context.getPackageName());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> localIntent;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Launch the application&#x27;s details settings.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">launchAppDetailsSettings</span><span class="params">(Context context ,String pkg)</span> </span>&#123;</span><br><span class="line">        Intent intent = <span class="keyword">new</span> Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);</span><br><span class="line">        intent.setData(Uri.parse(<span class="string">&quot;package:&quot;</span> + pkg));</span><br><span class="line">        <span class="keyword">if</span> (!isIntentAvailable( context ,intent)) <span class="keyword">return</span>;</span><br><span class="line">        context.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isIntentAvailable</span><span class="params">(Context context ,<span class="keyword">final</span> Intent intent)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> context.getApplicationContext().getPackageManager()</span><br><span class="line">                .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)</span><br><span class="line">                .size() &gt; <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>


<p>FileStoreHelper.java</p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.androidfile;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.graphics.Bitmap;</span><br><span class="line"><span class="keyword">import</span> android.graphics.BitmapFactory;</span><br><span class="line"><span class="keyword">import</span> android.os.Environment;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.BufferedInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.BufferedOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileNotFoundException;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> androidx.core.app.NavUtils;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * step1: 配置文件声明权限</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FileStoreHelper</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isExStoreUsable</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">storeTxt</span><span class="params">(String path, String txtStr)</span> </span>&#123;</span><br><span class="line">        FileOutputStream fos = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            fos = <span class="keyword">new</span> FileOutputStream(path);</span><br><span class="line">            fos.write(txtStr.getBytes());</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (fos != <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    fos.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">readTxt</span><span class="params">(String path)</span></span>&#123;</span><br><span class="line">        String readStr =<span class="string">&quot;&quot;</span>;</span><br><span class="line">        FileInputStream fis =<span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            fis = <span class="keyword">new</span> FileInputStream(path);</span><br><span class="line">            <span class="keyword">byte</span>[] bytes = <span class="keyword">new</span> <span class="keyword">byte</span>[fis.available()];</span><br><span class="line">            fis.read(bytes);</span><br><span class="line">            readStr = <span class="keyword">new</span> String(bytes);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (fis != <span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    fis.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> readStr;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">storeBitmap</span><span class="params">(String path , Bitmap bitmap ,<span class="keyword">int</span> quality)</span></span>&#123;</span><br><span class="line">        BufferedOutputStream bos= <span class="keyword">null</span>;</span><br><span class="line">        FileOutputStream fos=<span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            fos = <span class="keyword">new</span> FileOutputStream(path);</span><br><span class="line">            bos = <span class="keyword">new</span> BufferedOutputStream(fos);</span><br><span class="line">            bitmap.compress(Bitmap.CompressFormat.JPEG,quality ,bos);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (fos !=<span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    fos.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (bos != <span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    bos.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span>  <span class="keyword">static</span>  Bitmap <span class="title">loadBitmap</span><span class="params">(String path )</span></span>&#123;</span><br><span class="line">        Bitmap bitmap = <span class="keyword">null</span> ;</span><br><span class="line">        FileInputStream fis = <span class="keyword">null</span>;</span><br><span class="line">        BufferedInputStream bis = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            fis = <span class="keyword">new</span> FileInputStream(path);</span><br><span class="line">            bis = <span class="keyword">new</span> BufferedInputStream(fis);</span><br><span class="line">            bitmap = BitmapFactory.decodeStream(bis);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (fis != <span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    fis.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (bis != <span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    bis.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> bitmap;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>




  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/about">Cupster</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/2019/09/28/027-Android%E5%AD%98%E5%82%A8%EF%BC%882%EF%BC%89%EF%BC%9A%E6%96%87%E4%BB%B6&amp;Properties%E8%AF%A6%E8%A7%A3/">http://example.com/2019/09/28/027-Android%E5%AD%98%E5%82%A8%EF%BC%882%EF%BC%89%EF%BC%9A%E6%96%87%E4%BB%B6&amp;Properties%E8%AF%A6%E8%A7%A3/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2019/10/01/028-Android存储（3）：assets文件详解/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">028-Android存储（3）：assets文件详解 </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2019/09/19/026-Android存储（1）：Sharedpreference详解/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">026-Android存储（1）：Sharedpreference详解 </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-File%E5%AD%98%E5%82%A8%E4%B8%8E%E8%AF%BB%E5%8F%96"><span class="toc-text">2.File存储与读取</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-1%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%86%99%E5%85%A5File"><span class="toc-text">2.1字符串写入File</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-2%E8%AF%BB%E5%8F%96File%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">2.2读取File文件中的字符串</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-3%E5%AD%98%E5%82%A8%E5%9B%BE%E7%89%87"><span class="toc-text">2.3存储图片</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-4%E8%AF%BB%E5%8F%96%E5%9B%BE%E7%89%87"><span class="toc-text">2.4读取图片</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-Properties%E5%AD%98%E5%82%A8%E4%B8%8E%E8%AF%BB%E5%8F%96"><span class="toc-text">3.Properties存储与读取</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E5%85%B6%E4%BB%96%E7%9B%B8%E5%85%B3%E4%BB%A3%E7%A0%81"><span class="toc-text">4.其他相关代码</span></a></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/ic_head.jpg" class="author-img">

<p class="author-name">Cupster</p>
<p class="author-description">深山磨柴斧，一鸣震江湖</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>66</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>31</span>
    <span>标签</span>
  </a>
</div>

<div class="author-card-society">
  
    <div class="author-card-society-icon">
      <a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/">
        <i class="iconfont /img/ic_csdn.png society-icon"></i>
      </a>
    </div>
  
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-File%E5%AD%98%E5%82%A8%E4%B8%8E%E8%AF%BB%E5%8F%96"><span class="toc-text">2.File存储与读取</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-1%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%86%99%E5%85%A5File"><span class="toc-text">2.1字符串写入File</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-2%E8%AF%BB%E5%8F%96File%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">2.2读取File文件中的字符串</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-3%E5%AD%98%E5%82%A8%E5%9B%BE%E7%89%87"><span class="toc-text">2.3存储图片</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-4%E8%AF%BB%E5%8F%96%E5%9B%BE%E7%89%87"><span class="toc-text">2.4读取图片</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-Properties%E5%AD%98%E5%82%A8%E4%B8%8E%E8%AF%BB%E5%8F%96"><span class="toc-text">3.Properties存储与读取</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E5%85%B6%E4%BB%96%E7%9B%B8%E5%85%B3%E4%BB%A3%E7%A0%81"><span class="toc-text">4.其他相关代码</span></a></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/Android/">
        <div class="categories-list-item">
          Android
          <span class="categories-list-item-badge">38</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Dev-Base/">
        <div class="categories-list-item">
          Dev-Base
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/Kotlin/">
        <div class="categories-list-item">
          Kotlin
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/自动化脚本/">
        <div class="categories-list-item">
          自动化脚本
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="/tags/Android/" title="Android"><div class="tags-list-item">Android</div></a>
    
    <a href="/tags/Android自定义View/" title="Android自定义View"><div class="tags-list-item">Android自定义View</div></a>
    
    <a href="/tags/MacOS/" title="MacOS"><div class="tags-list-item">MacOS</div></a>
    
    <a href="/tags/Kotlin/" title="Kotlin"><div class="tags-list-item">Kotlin</div></a>
    
    <a href="/tags/Android持久化/" title="Android持久化"><div class="tags-list-item">Android持久化</div></a>
    
    <a href="/tags/CentOS/" title="CentOS"><div class="tags-list-item">CentOS</div></a>
    
    <a href="/tags/Linux/" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="/tags/Android事件/" title="Android事件"><div class="tags-list-item">Android事件</div></a>
    
    <a href="/tags/Android基础/" title="Android基础"><div class="tags-list-item">Android基础</div></a>
    
    <a href="/tags/Android手势/" title="Android手势"><div class="tags-list-item">Android手势</div></a>
    
    <a href="/tags/Scrcpy/" title="Scrcpy"><div class="tags-list-item">Scrcpy</div></a>
    
    <a href="/tags/Android动画/" title="Android动画"><div class="tags-list-item">Android动画</div></a>
    
    <a href="/tags/Git/" title="Git"><div class="tags-list-item">Git</div></a>
    
    <a href="/tags/Java/" title="Java"><div class="tags-list-item">Java</div></a>
    
    <a href="/tags/Docker/" title="Docker"><div class="tags-list-item">Docker</div></a>
    
    <a href="/tags/Android-Dialog/" title="Android Dialog"><div class="tags-list-item">Android Dialog</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-File%E5%AD%98%E5%82%A8%E4%B8%8E%E8%AF%BB%E5%8F%96"><span class="toc-text">2.File存储与读取</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-1%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%86%99%E5%85%A5File"><span class="toc-text">2.1字符串写入File</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-2%E8%AF%BB%E5%8F%96File%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">2.2读取File文件中的字符串</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-3%E5%AD%98%E5%82%A8%E5%9B%BE%E7%89%87"><span class="toc-text">2.3存储图片</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-4%E8%AF%BB%E5%8F%96%E5%9B%BE%E7%89%87"><span class="toc-text">2.4读取图片</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-Properties%E5%AD%98%E5%82%A8%E4%B8%8E%E8%AF%BB%E5%8F%96"><span class="toc-text">3.Properties存储与读取</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E5%85%B6%E4%BB%96%E7%9B%B8%E5%85%B3%E4%BB%A3%E7%A0%81"><span class="toc-text">4.其他相关代码</span></a></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-02</div>
        <a href="/2021/03/02/Git commit规范/"><div class="recent-posts-item-content">Git commit规范</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-02-16</div>
        <a href="/2021/02/16/AutoJS一文精通AutoJS脚本教程详解/"><div class="recent-posts-item-content">AutoJS一文精通AutoJS脚本教程详解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-21</div>
        <a href="/2021/01/21/架构师进阶：注解Annotation全解/"><div class="recent-posts-item-content">架构师进阶：注解Annotation全解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-06</div>
        <a href="/2021/01/06/ConstraintLayout详解/"><div class="recent-posts-item-content">ConstraintLayout详解</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">不负明天 </a>
      </div>
    </div>

    
    <div class="footer-dsc">
      
      Powered by
      <a href="https://hexo.io/" class="footer-link" target="_blank" rel="nofollow noopener noreferrer">&nbsp;Hexo </a>
      
      
      <span>&nbsp;|&nbsp;</span>
      
      
      Theme -
      <a href="https://github.com/theme-kaze" class="footer-link" target="_blank"
        rel="nofollow noopener noreferrer">&nbsp;Kaze</a>
      
    </div>
    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量686<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数219<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>